﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class UserControl_Login_Register : System.Web.UI.UserControl
{
    TblCustomerDAO customerDAO = new TblCustomerDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private bool UserAccount_CheckExists()
    {
        bool bExists = false;
        try
        {
            bExists = customerDAO.CheckExits(txt_email.Text, 1);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
        return bExists;
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        bool flag = false;
        lblMessage.Text = String.Empty;

        if (UserAccount_CheckExists())
        {
            lblMessage.Text = "Email is exist.";
            return;
        }
        try
        {
            TblCustomers customer = new TblCustomers();
            customer.CusID = 0;
            customer.CusAddress = txt_address.Text;
            customer.FName = txt_fistname.Text;
            customer.LName = txt_lastname.Text;
            customer.MobilePhone = txtmobilephone.Text;
            customer.Password = Encryption.EncryptToString(txtPassword.Text);
            customer.Postcode = "";
            customer.Suburb = "";
            customer.CusState = "";
            customer.EmailAdress = txt_email.Text;
            customer.HomePhone = "";
            customer.WorkPhone = "";
            customer.Company = "";
            customer.IsAdmin = false;
            customer.Notes = "";

            customerDAO.Insert_Up(customer, 1);
            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
        }
        if (flag)
        {
            ScriptManager.RegisterStartupScript(btnRegister, btnRegister.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        TblCustomers customer = customerDAO.GetCustomer(txtemailLogin.Text.Trim(), Encryption.EncryptToString(txtpass.Text));
        try
        {
            if ((customer.EmailAdress == txtemailLogin.Text) && (customer.Password == Encryption.EncryptToString(txtpass.Text)))
            {
                Session[Global.SESSION_LOGGED_USER] = customer;
                bFlag = true;

            }
            else
            {
                lbmsLogin.Text = "UserName or Password is not available.";
                lbmsLogin.Visible = true;
            }
        }

        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            // Response.Redirect(Global.ERROR_PAGE, true);
        }
        if (bFlag)
        {
            ScriptManager.RegisterStartupScript(btnLogin, btnLogin.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
        }
    }
}
